//
//  LoginViewController.m
//  atXiaoGeBC/Users/linyuniu/Desktop/TargetProtocollient
//
//  Created by Linyu Niu on 12/04/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "LoginViewController.h"
#import <CommonCrypto/CommonDigest.h>
#import "CompleteRegisterInfoViewController.h"

@interface LoginViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UITableView * loginTableView;
    NSString * phoneNumber;
    NSString * pwd;
}
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView * loginBgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_bg"]];
    [self.view addSubview:loginBgView];
    
    loginTableView = [self addTableViewInView:self.view frame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) delegateAndDataSource:self];
    [self addFooterView];
}


-(void)addFooterView{
    UIView * v = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    [self.view addSubview:v];
    UIView * hLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, v.frame.size.width, 1.0)];
    hLineView.backgroundColor = [UIColor colorWithRed:77.0 / 255.0 green:94.0 / 255.0 blue:107.0 / 255.0 alpha:1.0];
    [v addSubview:hLineView];
    
    UIButton * forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetButton addTarget:self action:@selector(forgetPassword) forControlEvents:UIControlEventTouchUpInside];
    forgetButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [forgetButton setTitleColor:[UIColor colorWithRed:77.0 / 255.0 green:94.0 / 255.0 blue:107.0 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    forgetButton.frame = CGRectMake(0, 0, v.frame.size.width / 2, v.frame.size.height);
    [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [v addSubview:forgetButton];
    
    UIButton * registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [registerButton addTarget:self action:@selector(registerAccount) forControlEvents:UIControlEventTouchUpInside];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerButton.frame = CGRectMake(v.frame.size.width / 2, 0, v.frame.size.width / 2, v.frame.size.height);
    [registerButton setTitle:@"马上注册" forState:UIControlStateNormal];
    [v addSubview:registerButton];
    
    UIView * vLineView = [[UIView alloc]initWithFrame:CGRectMake(v.frame.size.width / 2, 0, 1.0, v.frame.size.height)];
    vLineView.backgroundColor = [UIColor colorWithRed:77.0 / 255.0 green:94.0 / 255.0 blue:107.0 / 255.0 alpha:1.0];
    [v addSubview:vLineView];
}

-(void)forgetPassword{
    UIViewController * controller = [[CTMediator sharedInstance] forgetPwdViewController];
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)registerAccount{
    UIViewController * controller = [[CTMediator sharedInstance] registerViewController];
    [self.navigationController pushViewController:controller animated:YES];
}



-(void)loginUser{
    [self.view endEditing:YES];
    
#if kDevelopment
    phoneNumber = @"13916574944";
    
    
    
    //96E79218965EB72C92A549DD5A330112
    
    pwd = @"111111";
    
#endif
    if(phoneNumber.length != 0 && pwd.length != 0){
        [self startLoadingView:self.view];
        __weak LoginViewController * weakSelf = self;
        [[CTMediator sharedInstance] login:phoneNumber pwd:pwd block:^(NSDictionary * dic){
            [weakSelf stopLoadingView:weakSelf.view needShowMsg:YES result:dic];
            [[AppUtility shareInstance] saveUserData:dic];
            
            if([weakSelf isSuccess:dic]){
                [weakSelf loginStatus:[dic objectForKey:@"status"] userId:[dic objectForKey:@"courierId"]];
            }else{
                [weakSelf stopLoadingView:self.view needShowMsg:YES result:dic];
            }
        }];
    }
}

-(void)loginStatus:(NSString *)status userId:(NSString *)userId{
    if([status isEqualToString:@"UN_AUTH"]){
        UIViewController * controller = [[CTMediator sharedInstance] completeRegisterController:userId];
        [self.navigationController pushViewController:controller animated:YES];
    }else if([status isEqualToString:@"AUTH_DOING"]){
        
    }else if([status isEqualToString:@"VERIFI_COMPLETE"]){
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    if ([indexPath section] == 0) {
        cell.cellHeight = 100;
    }else if([indexPath section] == 1){
        if ([indexPath row] == 0) {
            UILabel * nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 40, 25)];
            nameLabel.font = [UIFont systemFontOfSize:14];
            nameLabel.text = @"+86";
            [nameLabel enlargeWidthToKeepFontSize];
            nameLabel.textColor = [UIColor whiteColor];
            [cell addSubview:nameLabel];
            
            UIView * vLine = [[UIView alloc]initWithFrame:CGRectMake(nameLabel.frame.origin.x + nameLabel.frame.size.width + 4, nameLabel.frame.origin.y + 3, 0.5, nameLabel.frame.size.height - 6)];
            vLine.backgroundColor = [UIColor whiteColor];
            [cell addSubview:vLine];
            UITextField * field = [[UITextField alloc]initWithFrame:CGRectMake(vLine.frame.origin.x + vLine.frame.size.width + 4, nameLabel.frame.origin.y, tableView.frame.size.width - 2 * nameLabel.frame.origin.x - 1 - 4, nameLabel.frame.size.height)];
            field.keyboardType = UIKeyboardTypeNumberPad;
            field.font = [UIFont systemFontOfSize:14];
            field.tag = [indexPath row];
            field.delegate = self;
            field.textColor = [UIColor whiteColor];//[UIColor colorWithRed:64.0 / 255.0 green:78.0 / 255.0 blue:89.0 / 255.0 alpha:1.0];
            [cell addSubview:field];
            
            
            UIColor *phoneHolderColor = [UIColor colorWithRed:82.0 / 255.0 green:98.0 / 255.0 blue:110.0 / 255.0 alpha:1.0];
            UIFont *phoneHolderFont = [UIFont systemFontOfSize:14];
            field.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"请输入您的手机号" attributes:@{NSForegroundColorAttributeName:phoneHolderColor,NSFontAttributeName:phoneHolderFont}];
            field.clearButtonMode = UITextFieldViewModeWhileEditing;
            
            
            
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y + nameLabel.frame.size.height + 10, tableView.frame.size.width - 2 * nameLabel.frame.origin.x, 1.0)];
            lineView.backgroundColor = [UIColor colorWithRed:77.0 / 255.0 green:94.0 / 255.0 blue:107.0 / 255.0 alpha:1.0];
            [cell addSubview:lineView];
            cell.cellHeight = lineView.frame.size.height + lineView.frame.origin.y + 10;
        }else if([indexPath row] == 1){
            UITextField * field = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, tableView.frame.size.width - 2 * 30, 25)];
            field.font = [UIFont systemFontOfSize:14];
            field.secureTextEntry = YES;
            field.textColor = [UIColor whiteColor];//[UIColor colorWithRed:64.0 / 255.0 green:78.0 / 255.0 blue:89.0 / 255.0 alpha:1.0];
            field.delegate = self;
            field.tag = [indexPath row];
            [cell addSubview:field];
            
            UIColor *phoneHolderColor = [UIColor colorWithRed:82.0 / 255.0 green:98.0 / 255.0 blue:110.0 / 255.0 alpha:1.0];
            UIFont *phoneHolderFont = [UIFont systemFontOfSize:14];
            field.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName:phoneHolderColor,NSFontAttributeName:phoneHolderFont}];
            field.clearButtonMode = UITextFieldViewModeWhileEditing;
            
            UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(field.frame.origin.x, field.frame.origin.y + field.frame.size.height + 10, field.frame.size.width, 1.0)];
            lineView.backgroundColor = [UIColor colorWithRed:77.0 / 255.0 green:94.0 / 255.0 blue:107.0 / 255.0 alpha:1.0];
            [cell addSubview:lineView];
            cell.cellHeight = lineView.frame.size.height + lineView.frame.origin.y + 10;
        }else if([indexPath row] == 2){
            
            
            UIButton * loginButton = [[CTMediator sharedInstance] commonOrangeButton:CGRectMake(30, 15, tableView.frame.size.width - 60, 40) title:@"登录" delegate:self action:@selector(loginUser)];
            
            /*
            // [UIButton buttonWithType:UIButtonTypeCustom];
            loginButton.backgroundColor = [UIColor colorWithRed:48.0 / 255.0 green:188.0 / 255.0 blue:106.0 / 255.0 alpha:1.0];
            loginButton.layer.cornerRadius = 5.0;
            loginButton.frame = CGRectMake(30, 15, tableView.frame.size.width - 60, 40);
            [loginButton setTitle:@"登录" forState:UIControlStateNormal];
            [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [loginButton addTarget:self action:@selector(loginUser) forControlEvents:UIControlEventTouchUpInside];
            loginButton.titleLabel.font = [UIFont systemFontOfSize:15];*/
            [cell addSubview:loginButton];
            cell.cellHeight = loginButton.frame.size.height + loginButton.frame.origin.y;
        }
    }else if([indexPath section] == 2){
        
    }
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if(section == 1){
        
        return 3;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}



-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag == 0) {
        phoneNumber = textField.text;
    }else if(textField.tag == 1){
        pwd = textField.text;
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 0) {
        NSString *getPhoneText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        
        if ([getPhoneText length] >= 11) {
            textField.text = [getPhoneText substringToIndex:11];
            return NO;
        }
    }else {
       
        
    }
    
    
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}
@end
